package com.brayan.webapp.model.usermgmt;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

/**
 * Entity implementation class for Entity: Role
 * 
 */
@Entity
public class Role implements Serializable {

	public enum RoleType {
		ADMIN, SUPERUSER, TENANT, VISITOR
	}

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	// do this for auto-insert / auto-increment in MySql
	private Long id;
	@Column(unique=true, nullable=false)
	private String role;
	private static final long serialVersionUID = 1L;

	@ManyToMany(mappedBy = "roles")
	private Set<User> users;

	private Date dateCreated;
	private Date dateModified;

	public Role() {
		super();
	}

	/***** EVENT HOOKS *****/

	@PrePersist
	protected void onCreate() {
		dateCreated = new Date();
	}

	@PreUpdate
	protected void onUpdate() {
		dateModified = new Date();
	}

	public Set<User> getUsers() {
		return users;
	}

	public void setUsers(Set<User> users) {
		this.users = users;
	}

	/***** GETTER AND SETTERS *****/

	public Long getId() {
		return this.id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getRole() {
		return this.role;
	}

	public void setRole(RoleType role) {
		this.role = role.toString();
	}

}
